<template>
  <div class="login">
    <Panel title="TCMP后台管理系统" :collapsible="false" :bodyStyle="{padding:'20px'}" >
      <Form ref="loginFormRef" :model="loginForm" :rules="loginRules" @validate="errors=$event">
        <div class="item">
          <div class="label">
            <Label for="username" align="top">账号:</Label>
            <!-- v-Tooltip="getTipOpts('username')" -->
            <TextBox inputId="username" name="username" v-model="loginForm.username" iconCls="icon-man"></TextBox>
          </div>
          <div class="error">{{getError('username')}}</div>
        </div>
        <div class="item">
          <div class="label">
            <Label for="password" align="top">密码:</Label>
            <!--v-Tooltip="getTipOpts('password')" -->
            <TextBox inputId="password" name="password" v-model="loginForm.password" iconCls="icon-lock"  @keyup.enter.native="handleLogin" ></TextBox>
          </div>
          <div class="error">{{getError('password')}}</div>
        </div>

        <div class="item">
          <div class="label">
            <Label for="code" align="top">验证码:</Label>
            <!-- v-Tooltip="getTipOpts('code')" -->
            <TextBox inputId="code" name="code" v-model="loginForm.code"  @keyup.enter.native="handleLogin"></TextBox>
            <img :src="codeUrl" @click="getCode" class="login-code-img" v-if="captchaEnabled" />
          </div>
          <div class="error">{{getError('code')}}</div>
        </div>

        <div class="other">
          <CheckBox inputId="rememberMe" name="rememberMe" v-model="loginForm.rememberMe"></CheckBox>
          <Label for="rememberMe">记住密码</Label>
        </div>
        <div class="btn">
          <LinkButton :disabled="loading" @click="handleLogin()">登 录</LinkButton>
        </div>
      </Form>
    </Panel>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2023 北京蓝海华腾科技有限公司 All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",//nrgly01//admin
        password: "",//123456//admin123
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: ["required", "length[5,10]"],
        password: ["required"],
        code: ["required"]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      redirect: undefined,
      errors:{}
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getError(value) {
      return this.errors[value] && this.errors[value].length
        ? this.errors[value][0]
        : null;
    },
    hasError(value) {
      return this.getError(value) != null;
    },
    // 右侧提示的
    getTipOpts(value) {
      return {
        content: this.getError(value),
        closed: !this.hasError(value),
        position: "right"
      };
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginFormRef.validate(valid => {
        if (valid) return
        this.loading = true;
        if (this.loginForm.rememberMe) {
          Cookies.set("username", this.loginForm.username, { expires: 30 });
          Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
          Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
        } else {
          Cookies.remove("username");
          Cookies.remove("password");
          Cookies.remove('rememberMe');
        }
        this.$store.dispatch("Login", this.loginForm).then(() => {
          this.$router.push({ path: this.redirect || "/" }).catch(() => { });
        }).catch(() => {
          this.loading = false;
          if (this.captchaEnabled) {
            this.getCode();
          }
        });
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.item{
  margin-bottom:20px;
  .label{
    display: flex;
    .textbox-label-top{
      width: 60px;
    }
    .f-field{
      width: 20em;
    }
  }
  .error{
    padding-left: 60px;
    color: red;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 30px;
  cursor: pointer;
  margin-left: 10px;
}
.other{
  margin-bottom:20px;
  display: flex;
  align-items: center;
  .textbox-label{
    margin-left: 10px;
  }
}
.btn{
  display: flex;
  .l-btn-small{
    width: 80%;
    margin: 0 auto;
  }
}
</style>
